package com.cw.demo.im.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cw.demo.im.server.MySocketServer;

@Controller
public class WebSocketController {

    @Autowired
    private MySocketServer socketServer;

    /**
     * 访问服务器端的统计信息页面
     */
    @RequestMapping("/server")
    public String onLineNum(Model model) {
        int count = socketServer.getOnlineNum();
        //把在线数放到model中，前端直接获取
        model.addAttribute("num", count);
        //服务端发送信息的页面
        return "server";
    }

    /**
     * 访问客户端首页
     */
    @RequestMapping("/client")
    public String index() {
        //跳转到客户端首页
        return "client";
    }

    @RequestMapping("/sendMessages")
    @ResponseBody
    public String sendMessages(String msg) {
        socketServer.sendMessages(msg);
        //如果该方法返回void，那么运行时会抛出org.thymeleaf.exceptions.TemplateInputException: Error resolving template
        return "server";
    }
    @RequestMapping("/sendMessagesToUser")
    @ResponseBody
    public String sendMessagesToUser(String userId,String msg) {
        socketServer.sendMessagesToUser(userId,msg);
        //如果该方法返回void，那么运行时会抛出org.thymeleaf.exceptions.TemplateInputException: Error resolving template
        return "server";
    }
}


